package com.example.demo.entity.res;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.annotation.JsonGetter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import com.example.demo.entity.table.LawsList;

@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Data
public class LawsDetailRes extends LawsList {
    private String directory;
    private String content;
    
    /**
     * 获取目录信息，如果是 JSON 字符串则解析为 JSON 对象
     */
    @JsonGetter("directory")
    public Object getDirectoryAsJson() {
        if (directory != null && !directory.isEmpty()) {
            try {
                // 尝试解析为 JSON 数组
                return JSON.parseArray(directory);
            } catch (Exception e) {
                try {
                    // 尝试解析为 JSON 对象
                    return JSON.parseObject(directory);
                } catch (Exception ex) {
                    // 如果都失败，返回原始字符串
                    return directory;
                }
            }
        }
        return directory;
    }
}